package edu.uoc.tdp.pac3.ui.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import edu.uoc.tdp.pac3.beans.Resource;
import edu.uoc.tdp.pac3.beans.Room;
import edu.uoc.tdp.pac3.client.Client;
import edu.uoc.tdp.pac3.ui.i18n.LanguageUtils;

public class ResourceRequestPanel extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;	

	private JButton saveButton;
	private JLabel roomLabel = new JLabel(LanguageUtils.getMessage("client.rooms.label"));
	private JLabel quantityLabel = new JLabel(LanguageUtils.getMessage("client.quantity.label"));
	private JLabel resTypeLabel = new JLabel(LanguageUtils.getMessage("client.resources.label"));
	private JComboBox roomCombo;
	private JTextField quantity;
	private JComboBox resourceCombo;
	private Client clientObj;
	
	public ResourceRequestPanel(Client client){
		clientObj = client;
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.insets = new Insets(2, 2, 2, 2);
		
		TitledBorder border = new TitledBorder(LanguageUtils.getMessage("client.panel.resourcerequest.title"));
		setBorder(border);
		
		roomCombo = new JComboBox();
		roomCombo.setPreferredSize(new Dimension(250,30));
		fillRoomsCombo();
		
		resourceCombo = new JComboBox();
		resourceCombo.setPreferredSize(new Dimension(250,30));
		fillResourceCombo();
		
		quantity = new JTextField();
		quantity.setPreferredSize(new Dimension(250,30));
		
		saveButton = new JButton();
		saveButton.setText(LanguageUtils.getMessage("client.button.save"));
		saveButton.addActionListener(this);

		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		JPanel room = new JPanel();
		room.setLayout(new FlowLayout());
		room.add(roomLabel);
		room.add(roomCombo);
		
		JPanel res = new JPanel();
		res.setLayout(new FlowLayout());
		res.add(resTypeLabel);
		res.add(resourceCombo);
		
		JPanel quant = new JPanel();
		quant.setLayout(new FlowLayout());
		quant.add(quantityLabel);
		quant.add(quantity);

		panel.add(room, BorderLayout.NORTH);
		panel.add(res, BorderLayout.CENTER);
		panel.add(quant, BorderLayout.SOUTH);
		
		constraints.gridx = 0; 
		constraints.gridy = 0; 
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		
		add(panel, constraints);
		
		constraints.gridx = 0; 
		constraints.gridy = 1; 
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		
		add(saveButton, constraints);
		
		
	}
	
	private void fillResourceCombo(){
		List<Resource> resList = null;
		try {
			resList = clientObj.getRemoteObject().getAllResources();
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		}	
		for(Resource r: resList){
			resourceCombo.addItem(r.getName());
		}
	}
	
	private void fillRoomsCombo(){
		List<Room> resList=null;
		try {
			resList = clientObj.getRemoteObject().getAllRooms();
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		}	
		for(Room r: resList){
			roomCombo.addItem(r.getDescription());
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==saveButton) {
			System.out.println("Save button pressed!");
			String resourceName = (String)resourceCombo.getSelectedItem();
			String roomName = (String)roomCombo.getSelectedItem();
			if(quantity.getText() != null && !"".equals(quantity.getText())){
				int newQuantity = new Integer(quantity.getText());
				try {
					int ok = clientObj.getRemoteObject().addResourceRequest(roomName, resourceName, newQuantity);
					switch (ok) {
					case 0:
						JOptionPane.showMessageDialog(null, 
								LanguageUtils.getMessage("client.info.message"),
								LanguageUtils.getMessage("client.info.title"),
								JOptionPane.INFORMATION_MESSAGE);
						break;
					case 1:
						JOptionPane.showMessageDialog(null, 
								LanguageUtils.getMessage("client.error.message.duplicatedReq"),
								LanguageUtils.getMessage("client.error.title"),
								JOptionPane.ERROR_MESSAGE);
						break;
					case -1:
						JOptionPane.showMessageDialog(null, 
								LanguageUtils.getMessage("client.error.message.generic"),
								LanguageUtils.getMessage("client.error.title"),
								JOptionPane.ERROR_MESSAGE);
						break;
					case 2:
						JOptionPane.showMessageDialog(null, 
								LanguageUtils.getMessage("client.error.message.morethan3"),
								LanguageUtils.getMessage("client.error.title"),
								JOptionPane.ERROR_MESSAGE);
						break;
						
					}
					quantity.setText("");
				} catch (RemoteException e1) {
					JOptionPane.showMessageDialog(null, 
							LanguageUtils.getMessage("client.error.message.remote"),
							LanguageUtils.getMessage("client.error.title"),
							JOptionPane.ERROR_MESSAGE);
				}
				
			}else{
				JOptionPane.showMessageDialog(null, 
						LanguageUtils.getMessage("client.error.message"),
						LanguageUtils.getMessage("client.error.title"),
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
